

<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        v-model="form"
        :table-loading="loading"
        :page="page"
        :data="data"
        :option="option"
        :permission="permissionList"
        :before-open="beforeOpen"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @row-del="rowDel"
        @size-change="sizeChange"
        @current-change="currentChange"
        @refresh-change="refreshChange"
        @search-change="searchChange"
        @search-reset="searchReset"
        @selection-change="selectionChange"
        @on-load="onLoad">

        <template slot="menuLeft">
          <el-button type="danger"
                     size="small"
                     icon="el-icon-delete"
                     plain
                     v-if="permissions.admin_tenant_add"
                     @click="handleDelete">删 除
          </el-button>
        </template>

      </avue-crud>

    </basic-container>
  </div>
</template>

<script>
  import {fetchPage, getObj, addObj, putObj, delObj} from '@/api/admin/tenant'
  import {option} from '@/const/crud/admin/tenant'
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        data: [],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        selectionList: [],
        form: {},
        query: {},
        loading: false,
        option: option
      }
    },
    computed: {
      ...mapGetters(["permissions"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permissions.admin_tenant_add, false),
          delBtn: this.vaildData(this.permissions.admin_tenant_del, false),
          editBtn: this.vaildData(this.permissions.admin_tenant_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {

      beforeOpen: function (show) {
        show()
      },

      onLoad(page, params) {
        fetchPage(Object.assign({
          current: page.currentPage,
          size: page.pageSize
        }, params, this.query)).then(response => {
          this.data = response.data.data.records
          console.log(this.data);
          this.page.total = response.data.data.total
        })
      },

      rowSave(row, done, loading) {
        addObj(this.form).then(() => {
          this.onLoad(this.page)
          done()
          this.$notify.success('创建成功')
        }).catch(() => {
          loading()
        })
      },

      rowUpdate(row, index, done, loading) {
        putObj(this.form).then(() => {
          this.onLoad(this.page)
          done()
          this.$notify.success('修改成功')
        }).catch(() => {
          loading()
        })
      },

      rowDel(row) {
        this.$confirm('是否确认删除ID为' + row.id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delObj(row.id)
        }).then(() => {
          this.onLoad(this.page)
          this.$notify.success('删除成功')
        })
      },

      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return delObj(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },

      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      refreshChange() {
        this.onLoad(this.page,this.query)
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize
      },
      currentChange(current) {
        this.page.currentPage = current
      },

    }
  };
</script>

<style lang="scss" scoped>
</style>

